package com.yc.config;

import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.ChatMessageDeserializer;
import dev.langchain4j.data.message.ChatMessageSerializer;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class PersistentMemoryStore implements ChatMemoryStore {
    private static final String REDIS_KEY_PREFIX = "chat:memory:";
    private final RedisTemplate<String, String> redisTemplate;

    @Autowired  // 确保使用构造器注入
    public PersistentMemoryStore(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
        // 验证序列化器
        if (!(redisTemplate.getKeySerializer() instanceof StringRedisSerializer)) {
            throw new IllegalStateException("RedisTemplate must use StringRedisSerializer");
        }
    }

    private String getKey(Object memoryId) {
        return REDIS_KEY_PREFIX + memoryId;
    }
    @Override
    public List<ChatMessage> getMessages(Object memoryId) {
        String key = getKey(memoryId);
        // 明确使用 String 类型的操作
        String messageJson = redisTemplate.opsForValue().get(key);
        if(messageJson == null || messageJson.isEmpty()){
            return List.of();
        }
        return ChatMessageDeserializer.messagesFromJson(messageJson);
    }

    @Override
    public void updateMessages(Object memoryId, List<ChatMessage> messages) {
        String key = getKey(memoryId);
        String messageJson = ChatMessageSerializer.messagesToJson(messages);
        // 明确使用 String 类型的操作
        redisTemplate.opsForValue().set(key, messageJson);
    }
//    @Override
//    public List<ChatMessage> getMessages(Object memoryId) {
//        String key = getKey(memoryId);
//        String messageJson = redisTemplate.opsForValue().get(key);
//        if(messageJson==null || messageJson.isEmpty()){
//            return  List.of();
//        }
//        return ChatMessageDeserializer.messagesFromJson(messageJson);
//    }
//
//    @Override
//    public void updateMessages(Object memoryId, List<ChatMessage> messages) {
//        String key = getKey(memoryId);
//        String messageJson = ChatMessageSerializer.messagesToJson(messages);
//        redisTemplate.opsForValue().set(key,messageJson);
//        //        try(Jedis jedis = jedisPool.getResource()) {
//        //            String messageJson = ChatMessageSerializer.messagesToJson(list);
//        //            jedis.set(key, messageJson);
//        //        }
//        //设置过期时间
//        //redisTemplate.expire(key,30,java.util.concurrent.TimeUnit.MINUTES);
//    }

    @Override
    public void deleteMessages(Object memoryId) {
        String key = getKey(memoryId);
        if(!redisTemplate.hasKey(key)){
            return;
        }
        redisTemplate.delete(key);
    }
}
